home *** CD-ROM | disk | FTP | other *** search
- /**
- --
- -- App: Bitmap Shape with Clip
- --
- --
- -- File: Bitmap Shape with Clip.c
- --
- --
- -- Comments: This code the shows off the ability of QuickDraw GX to clip any shape with any geometric shape.
- -- In this case, we retrieve a bitmap shape from the resource fork of the application,
- -- and clip it with a text shape - "BAY". We collect both shapes into a GX picture,
- -- thereby allowing us to make one call to draw both shapes.
- --
- --
- -- Version:
- -- 6/94 dkj Modified to match code in column in develop #19
- --
- --
- -- 1.0 4/94: added the capability to dynamically determine the amount of
- -- scaling required to make the clip shape clip the entire bitmap
- -- shape.
- --
- -- 3/93: created
- --
- -- Components: Bitmap Shape with Clip.c
- -- graphics shell.c
- -- graphics shell.h
- --
- --
- -- QuickDraw GX
- -- Libraries
- -- Used: This application uses the following QuickDraw GX library code files:
- -- "color library.c", "font library.c", "graphics debug library.c",
- -- "qd library.c", and "transform library.c".
- --
- --
- -- Notes: 1) Print this file in landscape for the best results
- -- 2) If you are using THINK C v5.x, I have added THINK markers to navigate the code.
- -- 3) This code was adapted from the "One Rectangle" QuickDraw GX sample.
- --
- --
- -- Author: Pete "Luke" Alexander
- -- Developer Technical Support
- -- AppleLink: DEVSUPPORT
- --
- --
- -- ©1992 - 1994 Apple Computer, Inc.
- --
- **/
-
- #include <events.h>
- #include <windows.h>
-
- #include "Font library.h"
- #include "graphics debugging.h"
- #include "graphics libraries.h"
- #include "graphics toolbox.h"
- #include "qd library.h"
- #include "graphics shell.h"
-
- #define kCheckBitmapShape // if defined, validates the bitmap shape
- //#define useDifferenceMethod // if defined, does it the long way
-
- // The id of the pixmap in the resource fork
- #define kPixMapID 128
-
- //
- // Set up the title and size of the window
- //
- Str255 gWindowTitle = "\p Nifty Clip: click to move on";
- Rect gWindowQDRect = {50, 20, 345, 455};
-
- //
- // gGraphicsHeapSize sets the size of the graphics gxHeap created by calling the GXNewGraphicsClient routine
- // in main () within graphics shell.c. You can determine the amount of graphics gxHeap required by using GraphicsBug.
- // With gGraphicsHeapSize set to 48k,I had 6 free blocks left in the graphics gxHeap and
- // I was not receiving any memory related warnings or notices from GX....
- //
- long gGraphicsHeapSize = 48;
-
- gxShape gthePicture;
-
- // •••••••••••• Added 5/31/94 dkj
- Boolean gShowSteps = true; // if true, shows intermediate steps
-
- /*------ ShowUntilClick ---------------------------------------------------------------------------------*/
- // Just shows the indicated shapes (max of two) and waits for a click before returning. A SickHack™.
- void ShowUntilClick(gxShape oneShape, gxShape otherShape);
-
- void ShowUntilClick(gxShape oneShape, gxShape otherShape)
- {
- SetPort(gWindow);
- EraseRect(&gWindow->portRect);
- if(oneShape != nil) GXDrawShape(oneShape);
- if(otherShape != nil) GXDrawShape(otherShape);
- while(!Button()); // Let user see it until they click
- while(WaitMouseUp()); // Wait 'til they let up
- }
-
- // ••••••••••••••• End additions
-
- /*------ DoInitialization ---------------------------------------------------------------------------------*/
-
- void DoInitialization(gWindow)
- WindowPtr gWindow;
- {
- gxShape theClip = nil, clipShapeOutline;
- gxShape theBitmap = nil;
- Fixed xScale, yScale;
- Fixed textWidth,textHeight;
- Fixed bitmapWidth, bitmapHeight;
- gxRectangle bitmapBounds, textBounds;
-
- InitCommonColors ();
-
- //
- // Create the picture shape to hold our clipped bitmap and outline (path) shape
- //
- gthePicture = GXNewShape(gxPictureType);
-
- //
- // Retrieve the bitmap shape form the resource fork of the application. With the "debugging" init installed, we
- // can check to see if the shape was retrieved correctly by calling the shape validation routine. If the shape is
- // not valid, we will recieve a warning in the debugger.
- //
- theBitmap = GetPixMapShape(kPixMapID);
-
- #ifdef kCheckBitmapShape
- GXValidateShape (theBitmap);
- #endif
-
- //
- // Define the text shape to clip our bitmap shape with. In this case, our clip shape
- // is a text shape (12 point Helvetica by default).
- //
- theClip = GXNewText( 3, (unsigned char*)"BAY", nil );
-
-
- // ••••• Added by dkj for develop
- if(gShowSteps)
- ShowUntilClick(theBitmap, theClip);
-
- #ifdef useDifferenceMethod
- //
- // Determine the bounds of our bitmap and clip shape and move the clip shape to the
- // top left corner of the bitmap shape.
- //
- GXGetShapeBounds( theBitmap, 0, &bitmapBounds );
- GXGetShapeBounds( theClip, 0, &textBounds );
- textHeight = textBounds.bottom - textBounds.top;
- GXMoveShapeTo ( theClip, bitmapBounds.left, bitmapBounds.top + textHeight );
-
- // ••••• Added by dkj for develop
- if(gShowSteps)
- ShowUntilClick(nil, theClip);
-
- //
- // We turn off the metrics and contour capabilites of TrueType to enable GX to
- // scale the clip shape linearly. We convert our text shape to a primitive shape because
- // you cannot use a text shape as a clip shape. A clip shape can only be a primitive shape
- //
- GXSetShapeTextAttributes ( theClip, gxNoMetricsGridText | gxNoContourGridText );
- GXPrimitiveShape ( theClip );
-
- //
- // We can now determine the amount we need to scale our clip shape to cover our bitmap
- // shape. This is accomplished by determining the differences between the height and width
- // of the bitmap and clip shapes. We will then use these differences as the scale factors
- // when we call GXScaleShape (..) to scale up our clip shape.
- //
- bitmapWidth = bitmapBounds.right - bitmapBounds.left;
- textWidth = textBounds.right - textBounds.left;
- xScale = FixedDivide( bitmapWidth, textWidth );
-
- bitmapHeight = bitmapBounds.bottom - bitmapBounds.top;
- textHeight = textBounds.bottom - textBounds.top;
- yScale = FixedDivide ( bitmapHeight, textHeight );
-
- GXScaleShape( theClip, xScale, yScale, bitmapBounds.left, bitmapBounds.top );
-
- #else
- GXSetShapeTextAttributes ( theClip, gxNoMetricsGridText | gxNoContourGridText );
- GXGetShapeBounds( theBitmap, 0, &bitmapBounds );
- GXSetShapeBounds( theClip, &bitmapBounds );
- #endif
-
- // ••••• Added by dkj for develop
- if(gShowSteps)
- ShowUntilClick(theBitmap, theClip);
-
- //
- // Set the clip of our bitmap shape to our text shape and add it to our picture.
- //
- GXSetShapeClip( theBitmap, theClip );
-
- // ••••• Added by dkj for develop
- if(gShowSteps)
- ShowUntilClick(theBitmap, nil);
-
- GXSetPictureParts(gthePicture, 0, 0, 1, &theBitmap, nil, nil, nil );
- GXDisposeShape ( theBitmap );
-
- //
- // Change the fill of our text shape be to the outline of the text,
- // set the size used, set the pen to cruise on the outside
- // of the contour of each letter, and add it to our picture shape.
- //
- GXSetShapeFill( theClip, gxClosedFrameFill );
- GXSetShapeStyleAttributes(theClip, gxOutsideFrameStyle);
- GXSetShapePen( theClip, ff(3));
- SetShapeCommonColor( theClip, blue);
-
-
- // ••••• Added by dkj for develop
- if(gShowSteps)
- ShowUntilClick(theClip, nil);
-
- GXSetPictureParts( gthePicture, 0, 0, 1, &theClip, nil, nil, nil );
- GXDisposeShape ( theClip );
-
- GXMoveShape(gthePicture, ff(20), ff(15) );
-
- // ••••• Added by dkj for develop
- if(gShowSteps)
- ShowUntilClick(gthePicture, nil);
- }
-
-
- /*------ DoDraw ---------------------------------------------------------------------------------------*/
-
- void DoDraw(gWindow)
- WindowPtr gWindow;
- {
- GXDrawShape (gthePicture);
- }
-
-
- /*------ DoDispose -------------------------------------------------------------------------------------*/
-
- void DoDispose(gWindow)
- WindowPtr gWindow;
- {
- /**
- You should always dispose of your GX graphics objects before tossing your window. Why? It's generally good
- form and this approach guarantees that everything is disposed. If you had not disposed of everything, the
- call to DisposeWindow should dispose of the objects. If you are running the debugging version of the
- SecretGraphics init with notices set, you will receive a notice that you had not disposed of everything. You
- can turn notices on in this file by setting gDebugging = TRUE (above).
- **/
- DisposeCommonColors ();
- GXDisposeShape(gthePicture);
- GXDisposeShape(gWindowBoundsShape);
- DisposeWindow(gWindow);
- }
-
-
-
- /*------ DoClick ---------------------------------------------------------------------------------------*/
-
- void DoClick( orgMouseLoc, theWindow )
- gxPoint orgMouseLoc;
- WindowPtr theWindow;
- {
- }
-
-
- /*------ DoIdle ----------------------------------------------------------------------------------------*/
-
- void DoIdle(gWindow)
- WindowPtr gWindow;
- {
- }
-